//
//  Fraction.m
//  7.5homework
//
//  Created by dllo on 16/7/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction

- (instancetype)initWithNumerator:(NSInteger)numerator
                      deniminator:(NSInteger)deniminator {
    self = [super init];
    if (self) {
        _deniminator = deniminator;
        _numerator = numerator;
    }
    return self;
}
+ (instancetype)fractionWithNumerator:(NSInteger)numerator
                          deniminator:(NSInteger)deniminator {
    return [[[Fraction alloc] initWithNumerator:numerator deniminator:deniminator]autorelease];
}
- (NSString *)description {
    return [NSString stringWithFormat:@"%ld/%ld",_numerator,_deniminator];
}
- (NSInteger)gcdFirstNumber:(NSInteger)firstNumber
               secondNumber:(NSInteger)secondNumber {
    while (secondNumber != 0 ) {
        NSInteger temp = firstNumber % secondNumber;
        firstNumber = secondNumber;
        secondNumber = temp;
    }
    return firstNumber;
}
- (void)reduce {
    NSInteger number = [self gcdFirstNumber:_numerator secondNumber:_deniminator];
    _numerator /= number;
    _deniminator /= number;
}
- (Fraction *)addFraction:(Fraction *)anotherFration {
    Fraction *fraction = [[Fraction alloc] init];
    fraction.numerator = _numerator * anotherFration.deniminator + _deniminator * anotherFration.numerator;
    fraction.deniminator = _deniminator * anotherFration.deniminator;
    [fraction reduce];
    return fraction;
}
- (Fraction *)delFraction:(Fraction *)anotherFration {
    Fraction *fraction = [[Fraction alloc] init];
    fraction.numerator = _numerator * anotherFration.deniminator - _deniminator * anotherFration.numerator;
    fraction.deniminator = _deniminator * anotherFration.deniminator;
    [fraction reduce];
    return fraction;
}
- (Fraction *)mulFraction:(Fraction *)anotherFration {
    Fraction *fraction = [[Fraction alloc] init];
    fraction.numerator = _numerator * anotherFration.numerator;
    fraction.deniminator = _deniminator * anotherFration.deniminator;
    [fraction reduce];
    return fraction;
}
- (Fraction *)divFraction:(Fraction *)anotherFration {
    Fraction *fraction = [[Fraction alloc] init];
    fraction.numerator = _numerator * anotherFration.deniminator;
    fraction.deniminator = _deniminator * anotherFration.numerator;
    [fraction reduce];
    return fraction;
}
- (void)comFraction:(Fraction *)anotherFration {
    Fraction *result = [self delFraction:anotherFration];
    if (result.numerator == 0) {
        NSLog(@"相等");
    } else if (result.deniminator > 0) {
        NSLog(@"%@ > %@",self,anotherFration);
    } else if (result.deniminator < 0) {
        NSLog(@"%@ < %@",self,anotherFration);
    }
}


@end
